﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using CADProyectoSIETE;

namespace ProyectoSIETE
{
    public partial class frmNuevoObjetoTaberna : Form
    {
        DataGridView dgvTaberna;

        public frmNuevoObjetoTaberna()
        {
            InitializeComponent();
        }

        public frmNuevoObjetoTaberna(ref DataGridView dgv)
        {
            InitializeComponent();
            dgvTaberna = dgv;
        }

        private void frmNuevoObjetoTaberna_Load(object sender, EventArgs e)
        {
            try
            {
                TabernaCAD taberna = new TabernaCAD();
                DataTable tabla = new DataTable();       
                //tabla = taberna.obtenerObjetosNoTaberna();
                dgvItems.DataSource = taberna.obtenerObjetosNoTaberna();
                dgvItems.DataMember = "objetos";
                dgvItems.Columns[0].Visible = false;
                dgvItems.Columns[1].HeaderText = "Nombre";
                dgvItems.Columns[2].HeaderText = "Descripcion";
                dgvItems.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
            catch (Exception ex) 
            {
                MessageBox.Show("Error al cargar los datos","Error");
            }

        }

        private void tbBuscador_MouseClick(object sender, MouseEventArgs e)
        {
            tbBuscador.Text = "";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                ObjetoCAD objCAD = new ObjetoCAD();
                dgvItems.DataSource = objCAD.buscarObjetosTaberna(tbBuscador.Text.ToString());
            }
            catch (Exception ex) {
                MessageBox.Show("Error al buscar","Error",MessageBoxButtons.OK);
            }

        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            try
            {
                TabernaCAD taCAD = new TabernaCAD();
                int aux = int.Parse(lbIdObjeto.Text.ToString());
                taCAD.insertar(aux, (int)nudPrecio.Value);
                nudPrecio.Value = 1;
                TabernaCAD taberna = new TabernaCAD();
                DataTable tabla = new DataTable();
                //tabla = taberna.obtenerObjetosNoTaberna();
                dgvItems.DataSource = taberna.obtenerObjetosNoTaberna();
                dgvItems.DataMember = "objetos";
                dgvItems.Columns[0].Visible = false;
                dgvItems.Columns[1].HeaderText = "Nombre";
                dgvItems.Columns[2].HeaderText = "Descripcion";
                dgvItems.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                MessageBox.Show("Objeto insertado","");
            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.ToString()+"Error al añadir el objeto","Error");
            }
        }

        private void dgvItems_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex > -1)
                {
                    lbIdObjeto.Text = dgvItems.Rows[e.RowIndex].Cells[0].Value.ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al seleccionar el objeto", "Error", MessageBoxButtons.OK);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmNuevoObjetoTaberna_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                TabernaCAD taberna = new TabernaCAD();
                dgvTaberna.DataSource = taberna.obtenerObjetosTaberna();
                //form.actualizarDgv();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "");
            }
        }
    }
}
